<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <title></title>
    <style>
        * {
            font-size: 16px !important;
            width: 100% !important;
            overflow-x: hidden;
        }

        .hkm-title {
            font-size: 22px !important;
            line-height: 1.4;
            margin-top: 20px;
            margin-bottom: 15px;
        }

        .hkm-info {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            line-height: 2;
            margin-bottom: 2rem;
        }

        .hkm-info>span {
            margin-right: 10px;
            width: auto !important;
        }

        .hkm-author {
            color: rgba(0, 0, 0, 0.3);
            display: inline-block;
            vertical-align: middle;
            font-size: 15px !important;
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        }

        .hkm-yc {
            display: none;
            vertical-align: middle;
            padding: 0 4px;
            font-size: 12px !important;
            line-height: 1.67;
            border-radius: 2px;
            -moz-border-radius: 2px;
            -webkit-border-radius: 2px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            word-wrap: normal;
            font-style: normal;
            letter-spacing: normal;
            background: rgba(0, 0, 0, 0.05);
            color: rgba(0, 0, 0, 0.3);
        }

        .hkm-name {
            color: #576b95;
            font-size: 15px !important;
        }

        .hkm-time {
            font-style: normal;
            color: rgba(0, 0, 0, 0.3);
            display: inline-block;
            vertical-align: middle;
            font-size: 15px !important;
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        }

        .hkm-content {
            width: 95% !important;
            padding: 0 8px;
        }

        .container {
            margin-left: 10px;
            width: 90% !important;
            padding-bottom: 20px;
        }
        .hkm-link {
            margin-top: 15px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="hkm-title">

        </div>
        <p class="hkm-info">
            <span class="hkm-yc">原创</span>
            <span class="hkm-author">

            </span>
            <span class="hkm-name">

            </span>
            <span class="hkm-time"></span>
        </p>
        <div class="hkm-content">
        </div>
        <div class="hkm-link"></div>
    </div>
    <script src="./js/jquery-3.0.0.min.js"></script>
    <script src="./js/bootstrap.bundle.min.js"></script>
    <script type="text/javascript" src="https://cdn.wulabh.com/ptsc/1.0.0/ptsc.min.js"></script>
    <script type="text/javascript" src="https://cdn.wulabh.com/jquery/2.1.4/jquery.min.js"></script>
    <script>
        var NowTime;
        var NowTime2;
        var readTime;
        var code, href, appid, info, id, type, reading_duration, read_id, user_id;

        pushHistory();
        window.addEventListener("popstate", function (e) {
            if (typeof (WeixinJSBridge) != "undefined") {
                WeixinJSBridge.call('closeWindow');
            } else {
                if (navigator.userAgent.indexOf("MSIE") > 0) {
                    if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                        window.opener = null;
                        window.close();
                    } else {
                        window.open('', '_top');
                        window.top.close();
                    }
                } else if (navigator.userAgent.indexOf("Firefox") > 0) {
                    // window.location.href = 'about:blank ';
                    window.history.go(-2);
                } else {
                    window.opener = null;
                    window.open('', '_self', '');
                    window.close();
                }
            }
        }, false);

        function pushHistory() {
            var state = {
                title: "title",
                url: ""
            };
            window.history.pushState(state, "title", "");
        }
        window.onbeforeunload = function () {
            return;
        }
        //监听页面关闭、销毁

        function getUrlCode() { // 截取url中的code方法
            var url = location.search
            var theRequest = new Object()
            if (url.indexOf("?") != -1) {
                var str = url.substr(1)
                var strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
                }
            }
            localStorage.setItem('huankemao_info', JSON.stringify(theRequest));
        };

        function getText(data) {
            var datas = data.split('+');
            var that = this;
            $.ajax({
                url: '../admin.php/get_temporary_preview',
                type: "post",
                data: {
                    code: datas[0],
                    type: datas[1] == "undefined" ? '' : datas[1]
                },
                success: function (res) {
                    let data = JSON.parse(res);
                    if (data.code == 200) {
                        $('.hkm-yc').css('display', 'block');
                        if (data.data.link) {
                            $('.hkm_link').html('<a href=' + data.data.link + '>阅读原文，点击链接</a>');
                        }
                        $('.hkm-title').html(data.data.title);
                        $('.hkm-author').html(data.data.user ? data.data.user : data.data.phone);
                        $('.hkm_time').html(data.data.create_at);
                        $('.hkm_content').html(data.data.content);
                        $('.hkm-name').html(data.data.company_name);
                        if (datas[1]) {
                            that.SaveInfo();
                        }
                    }
                }
            });

        }

        function SaveInfo() {
            var arr = JSON.parse(localStorage.getItem('huankemao_info')).state.split('+');
            var that = this;
            $.ajax({
                url: '../admin.php/add_article_reading',
                type: "post",
                data: {
                    content_id: arr[0],
                    openid: info.openid,
                    staff_user_id: arr[2],
                    headimgurl: info.headimgurl,
                    nickname: info.nickname,
                    gender: info.sex,
                    province: info.province,
                    city: info.city,
                    reading_time: readTime,
                    reading_duration: '1'
                },
                success: function (res) {
                    read_id = JSON.parse(res).data;
                    if (JSON.parse(res).code == 200) {
                        setInterval(() => {
                            that.getreadTime()
                        }, 1000);
                    }

                }
            });
        }

        function getreadTime() {
            NowTime2 = new Date().getTime();
            reading_duration = Math.round((NowTime2 - NowTime) / 1000)
            $.ajax({
                url: '../admin.php/add_article_reading',
                type: "post",
                data: {
                    id: read_id,
                    reading_duration: reading_duration
                },
                success: function (res) {
                    console.log(res)
                }
            });
        }

        function formatDateTime(date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            h = h < 10 ? ('0' + h) : h;
            var minute = date.getMinutes();
            minute = minute < 10 ? ('0' + minute) : minute;
            var second = date.getSeconds();
            second = second < 10 ? ('0' + second) : second;
            return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
        }
        window.onload = function () {

            window.onscroll = function () {
                //为了保证兼容性，这里取两个值，哪个有值取哪一个
                //scrollTop就是触发滚轮事件时滚轮的高度
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                scrollTop >= 50 ? document.title = document.querySelector('.hkm-name').innerText : document.title = '';
            }

            this.getUrlCode();
            var u = navigator.userAgent;
            // var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
            // var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
            code = JSON.parse(localStorage.getItem('huankemao_info')).code;
            id = JSON.parse(localStorage.getItem('huankemao_info')).id;
            type = JSON.parse(localStorage.getItem('huankemao_info')).type;
            user_id = JSON.parse(localStorage.getItem('huankemao_info')).userid;
            NowTime = new Date().getTime();
            readTime = this.formatDateTime(new Date()); //获取离开时间

            var that = this;
            if (code) {
                $.ajax({
                    url: '../admin.php/wechat_login',
                    type: "get",
                    data: {
                        code: code
                    },
                    success: function (res) {
                        var res = JSON.parse(res);
                        if (res.code == '200') {
                            info = res.data;
                            localStorage.setItem("info", JSON.stringify(info));
                            that.getText(JSON.parse(localStorage.getItem('huankemao_info')).state)
                        }
                    }
                });
            } else {
                this.getHref()
            }
        }
        
        function getHref() {
            $.ajax({
                url: '../admin.php/wechat_login',
                type: "get",
                data: {
                    code: ''
                },
                success: function (res) {
                    var res = JSON.parse(res);
                    if (res.code == '200') {
                        appid = res.data;
                        href =
                            `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(window.location.href.split('?')[0])}&response_type=code&scope=snsapi_userinfo&state=${id}+${type}+${user_id}#wechat_redirect`;
                        window.location.href = href;
                    }
                }
            });
        }
    </script>
</body>

</html>